
const alertOnlineStatus = () => {
	//window.alert(navigator.onLine ? 'online' : 'offline')
	// 监听网络的连接状态, 联网不代表能访问到服务端
	let msg = navigator.onLine ? 'online' : 'offline'
	ipcRenderer.send('app-onlinemessage', msg)
}

//推送内容展示的数据
function pushLoadContentPage(loadPage) {
	if(loadPage["contentlist"]){
		for (var i=0;i< loadPage["contentlist"].length;i++) {
			loadPage["contentlist"][i]["url"] = VisualConst.appendDeviceParam(loadPage["contentlist"][i]["url"]);
		}
	}
	//loginfo("替换设备位置参数:" + JSON.stringify(loadPage));
	ipcRenderer.send('app-loadContentPage', loadPage)
}

function clientIndexEvent(e) {
 	if (e["data"]["eventkey"] == "SCREENSHOT") {
		clientDesktopCapturer({}, function(picdata) {
			//图片的base64 地址
			//console.log(picdata)
			VisualReq.eventCallback(e, picdata, function(re) {
				//console.log(re);
			});
		})
	} else if (e["data"]["eventkey"] == "RESTARTAPP") {
		ipcRenderer.send('app-clientEvent', e);
	}else if (e["data"]["eventkey"] == "RELOADSCREEN") {
		//重新加载大屏内容
		reConnectOpen();
	}
}
//收到服务端推送的消息时
function clientIndexHandleMessage(data) {
	switch (data.pushtype) {
		case 'screens':
			//展示内容
			VisualConst.setLastContent(data);
			pushLoadContentPage(data);
			break;
		case 'event':
			//用来扩展 比如 要求客户端截图 ，获取客户端内存信息,主动触发更新 等场景
			clientIndexEvent(data);
			break;
	}
}

function connectWsServer() {
	var wsParams = VisualConst.getAppCfg()['ws'];
	var serverInfo = VisualConst.getServerInfo();
	// console.log("serverInfo",serverInfo,wsParams);
	// console.log("____clientStaticData",____clientStaticData);
	
	wsParams["ws_ip"] = serverInfo["ip"] || wsParams["ws_ip"];
	wsParams["ws_port"] = serverInfo["port"] || wsParams["ws_port"];
	wsParams["token"] = serverInfo["token"] || "";
	wsParams['deviceip'] = ____clientStaticData["network"]["address"];
	// ws_ip=127.0.0.1
	// ws_port=25002
	// ws_protocol=ws
	wsParams['deviceid'] = VisualConst.getCustomDevice({})["deviceid"];//____clientStaticData["_customDevice"]["deviceid"];
	wsParams['reconnInterval'] = 2000;
	wsParams["appid"] = VisualConst.getAppCfg()['comm']['appid'];
	initScreenWs(wsParams); //链接服务
}

function setStatusText(status) {
	$("#client_status").html(status);
}

function clientIndexHandleWsConn(event, e, ws) {
	//"onclose" onopen
	if (event == "onclose") {
		//1，关闭展示内容

		//2，设置状态显示
		setStatusText("已断开连接");
	} else if (event == "onopen") {
		var msg = {
			errcode: '100000',
			errmsg: 'open websocket'
		};
		ipcRenderer.send('app-socket-onlinemessage', msg);
		//重新加载客户端信息
		reConnectOpen();
	} else if (event == "onerror") {
		var msg = {
			errcode: '100500',
			errmsg: 'close websocket'
		};
		ipcRenderer.send('app-socket-onlinemessage', msg)
		setStatusText("已断开连接");
	}
};
